<template>
  <CompOperate
      :field="field"
      :designer="designer"
      :design-state="designState"
      :parent-list="parentList"
      :parent-widget="parentWidget"
      :sub-form-row-id="subFormRowId"
      :sub-form-row-index="subFormRowIndex"
      :sub-form-col-index="subFormColIndex"
      :index-of-parent-list="indexOfParentList"
      :display-style="field['options'].displayStyle"
  >
    <div class="scene-DRC-TCM-info-widget" :style="getStyle('head')">
      <div class="DRC-TCM-top">
        <div class="logo-wrap">
          <template v-for="item in optionsInfo.hospitalLogo">
            <img class="logo" v-if="!!pictureResource(item,'logo')" :src="pictureResource(item,'logo')"
                 :alt="item.label"/>
          </template>
        </div>
        <div class="code-info">
          <template v-for="(item,index) in optionsInfo.hospitalCode">
            <div class="code-item" :class="`code-${index+1}`" v-if="!!pictureResource(item,'code')">
              <div class="code-picture">
                <img class="logo" :src="pictureResource(item,'code')" :alt="item.label"/>
              </div>
              <div class="code-name">{{ item.name || "\u00a0" }}</div>
            </div>
          </template>
        </div>
      </div>
      <div class="DRC-TCM-info">
        <div class="dept-room-number">
          <div class="content">
            <ScrollEditX :content="getRoomContent" :key="widgetKey"/>
          </div>
        </div>
        <div class="doctor-patient-info" v-for="(item,index) in getRoomDoctorInfo" :key="index">
          <div class="doctor-info">
            <div class="doctor-brief">
              <div class="left">
                <img :src="getDoctorPicture(item)" alt="医生" @error='handleError'/>
              </div>
              <div class="right">
                <div class="name" :style="getStyle('doctorName')">
                  <ScrollX>{{ (item.workerName) || '\u00a0' }}</ScrollX>
                </div>
                <div class="tag-info">
                  <template v-if="!!(item?.workerTitle)">
                    <div class="item doctor-level">
                      <ScrollX>{{ (item.workerTitle) || '\u00a0' }}</ScrollX>
                    </div>
                  </template>
                </div>
              </div>
            </div>
            <div class="doctor-describe" :style="getStyle('doctorDescribe')">
              <!-- 组件接受两个参数，content：滚动的文字内容；pageDuration滚动一页需要的时间 -->
              <ScrollY :content="(item.workerIntro) || '\u00a0'" :pageDuration="10"/>
            </div>
          </div>
          <div class="patient-info">
            <div class="patient-item current-patient">
              <div class="patient-sign" :style="getStyle('patientSign')">
                {{ optionsInfo['currentPatientContent'] || '\u00a0' }}
              </div>
              <div class="patient-content" :style="getStyle('patientContent')">
                <template v-for="(item) in getPatientList(item,'current')">
                  {{ item.orderNo || '\u00a0' }}
                  {{ item.patientName || '\u00a0' }}
                </template>
              </div>
            </div>
            <div class="patient-item wait-patient">
              <div class="patient-sign" :style="getStyle('patientSign')">
                {{ optionsInfo['waitingPatientContent'] || '\u00a0' }}
              </div>
              <div class="patient-content" :style="getStyle('patientContent')">
                <template v-for="(item) in getPatientList(item,'wait')">
                  {{ item.orderNo || '\u00a0' }}
                  {{ item.patientName || '\u00a0' }}
                </template>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </CompOperate>
</template>

<script>
import {v4 as uuid} from 'uuid';
import {pxToVw} from '@/utils/common';
import emitter from "@/utils/designer/emitter";
import {buildParentApi} from "@/utils/designer/util";
import ScrollX from '@c/public/seamlessScroll/ScrollX.vue';
import ScrollEditX from "@c/public/seamlessScroll/ScrollEditX.vue";
import ScrollY from '@c/public/seamlessScroll/ScrollY.vue';
import mixin from "@sc/templateDetails/modules/homeMiddle/fieldMixin";
import CompOperate from "@c/public/form-designer/devType/modules/compOperate.vue";

let buildParent = buildParentApi();
export default {
  name: "scene-DRC-TCM-info-widget",

  componentName: "FieldWidget", //必须固定为FieldWidget，用于接收父级组件的broadcast事件

  mixins: [emitter, mixin],

  components: {
    CompOperate,
    ScrollY,
    ScrollX,
    ScrollEditX,
  },

  props: {
    widget: Object,
    devType: {
      type: Boolean,
      default: false,
    },
    field: Object,
    parentWidget: Object,
    parentList: Array,
    indexOfParentList: Number,
    designer: Object,
    designState: {
      type: Boolean,
      default: false,
    },
    subFormRowIndex: {
      /* 子表单组件行索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormColIndex: {
      /* 子表单组件列索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormRowId: {
      /* 子表单组件行Id，唯一id且不可变 */ type: String,
      default: "",
    },
    headTitle: {
      type: String,
      default: "",
    },
    visibleTitle: {
      type: Boolean,
    },
    //prop传入的表单JSON配置
    parentApi: {
      type: Object,
      default: () => buildParentApi()
    },
  },

  data() {
    return {
      widgetKey: uuid(),
      optionsInfo: Object(),
      header: require("@a/images/design/header.jpeg"),
      logo: require("@a/images/design/logox2.png"),
      pause: require("@a/images/design/pause.png"),
      code: require("@a/images/design/placeholder-code.png"),
    }
  },

  computed: {
    // 设置的行内样式
    getStyle() {
      return function (val) {
        const self = this;
        let info = (self.optionsInfo) || Array();
        let Obj = Object();
        switch (val) {
          case 'head':
            Reflect.set(Obj, 'height', pxToVw(info.headHeight));
            Reflect.set(Obj, 'backgroundColor', `${(info.backgroundColor)}`);
            return Obj;
          case 'doctorName':
            Reflect.set(Obj, 'fontSize', pxToVw(info.doctorNameFontSize));
            Reflect.set(Obj, 'fontWeight', (info.doctorNameFontWeight) || 'normal');
            return Obj;
          case 'doctorDescribe':
            Reflect.set(Obj, 'fontSize', pxToVw(info.doctorDescribeFontSize));
            Reflect.set(Obj, 'fontWeight', (info.doctorDescribeFontWeight) || 'normal');
            return Obj;
          case 'patientSign':
            Reflect.set(Obj, 'fontSize', pxToVw(info.patientSignFontSize));
            Reflect.set(Obj, 'fontWeight', (info.patientFontWeight) || 'normal');
            return Obj;
          case 'patientContent':
            Reflect.set(Obj, 'fontSize', pxToVw(info.patientContentFontSize));
            Reflect.set(Obj, 'fontWeight', (info.patientFontWeight) || 'normal');
            return Obj;
        }
      }
    },

    // 获取医生头像
    getDoctorPicture() {
      return function (row) {
        let isPause = (row?.isPause);
        let header = '/tpleditor/resource/assets/images/header.png';
        if (!isPause) {
          return (row.workerPicture) || header
        } else {
          return this.pause;
        }
      }
    },

    // 获取科室信息
    getRoomDeptInfo() {
      let devType = (this.devType);
      let roomDoctorInfo = this.$store.getters['getRoomDeptInfo'];
      if (devType) {
        return roomDoctorInfo.apiData;
      } else {
        return roomDoctorInfo.mockData;
      }
    },

    // 获取全量医患信息
    getRoomDoctorInfo() {
      let devType = (this.devType);
      let roomDoctorInfo = this.$store.getters['getRoomDoctorInfo'];
      let list = devType ? (roomDoctorInfo.apiData) : (roomDoctorInfo.mockData)
      if (!!(list?.length)) {
        return list.slice(0, 1);
      } else {
        return Array(1).fill({});
      }
    },

    // 诊室号-科室-号别-诊室
    getRoomContent() {
      const self = this;
      let {comb, checkList, deptNameKey, roomNameKey, combOptions} = self.optionsInfo;
      let roomInfo = self.getRoomDeptInfo;
      if (comb) {
        let deptName = deptNameKey ?
            `<span class='dept-number'>${!!(roomInfo[deptNameKey]) ? roomInfo[deptNameKey] : '\u00a0'}</span>` :
            `<span class='dept-number'>${roomInfo?.deptName || '\u00a0'}</span>`;
        let roomName = roomNameKey ?
            `<span class='room-number'>${!!(roomInfo[roomNameKey]) ? roomInfo[roomNameKey] : '\u00a0'}</span>` :
            `<span class='room-number'>${roomInfo?.roomName || '\u00a0'}</span>`;
        return `${roomName}${deptName}`
      } else {
        let str = '';
        combOptions.forEach((item) => {
          let result = checkList.includes((item.value));
          if (result) {
            let currentInfo = roomInfo[item.value]
            if (!!currentInfo) {
              str = `${self.$lodash.trim(str)}${currentInfo}`
            }
          }
        })
        return str
      }
    },

    // 患者截取
    getPatientList() {
      return function (row, type) {
        let {waitMode} = (this.optionsInfo);
        switch (type) {
          case 'current':
            let currentInfo = row?.treatingPatient || [];
            return (!!currentInfo && !!(currentInfo?.length)) ? currentInfo.slice(0, waitMode) : []
          case 'wait':
            let waitInfo = row?.waitingPatientList || [];
            return (!!waitInfo && !!(waitInfo?.length)) ? waitInfo.slice(0, waitMode) : []
        }
      }
    },

    //获取LOGO资源
    pictureResource() {
      return function (row, type) {
        switch (type) {
          case 'logo':
            if (this.$lodash.isObject(row)) {
              return row?.logoInfo?.val?.url
            } else {
              return row.imageUrl || this.logo
            }
          case 'code':
            if (this.$lodash.isObject(row)) {
              return row?.codeInfo?.val?.url
            } else {
              return row.imageUrl || this.logo
            }
        }
      }
    },
  },

  created() {
    this.registerToRefList();
    this.initEventHandler();
    this.handleOnCreated();
  },

  mounted() {
    this.handleOnMounted();
  },

  methods: {
    // 接收父组件传递过来的数据
    executeMethod(key, data) {
      switch (key) {
        case "roomDeptInfo":
          this.$store.dispatch('setRoomDeptInfo', data)
          break
        case "roomDoctorInfo":
          this.$store.dispatch('setRoomDoctorInfo', this.$lodash.cloneDeep(data))
          break
      }
    },

    handleError(event) {
      event.target.src = '/tpleditor/resource/assets/images/header.png'
    }
  },


  watch: {
    field: {
      handler: function (val) {
        let key = val.options.config;
        this.widgetKey = uuid();
        this.optionsInfo = val.options[key];
      },
      deep: true,
      immediate: true
    }
  },

  destroyed() {
    this.unregisterFromRefList();
  },

};
</script>

<style lang="scss" scoped>
.scene-DRC-TCM-info-widget {
  display: flex;
  flex-direction: column;
  background: transparent;
  box-sizing: border-box;
  overflow: hidden;
  font-size: 80px;
  @include background('design/1-16×2', 'left', 'center', 'auto', 'auto');

  .DRC-TCM-top {
    height: 166px;
    padding: 0 30px;
    display: flex;
    overflow: hidden;
    align-items: center;
    align-content: center;
    flex-direction: row;

    .logo-wrap {
      display: flex;
      align-items: center;
      flex-shrink: 0;
      justify-content: space-between;
      width: calc(100% - 250px - 24px);
      height: 125px;

      img {
        height: 100% !important;
        max-height: 90px !important;
        max-width: 100% !important;
        object-fit: contain;
      }
    }

    .code-info {
      display: flex;
      flex-shrink: 0;
      font-size: 24px;
      width: 274px;
      flex-direction: row;

      .code-item {
        width: 134px;
        height: 134px;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        align-self: center;
        background: rgba(138, 106, 54, 0.35);
        border-radius: 6px;
        padding: 8px;
        overflow: hidden;
        box-sizing: content-box;
        margin-top: 16px;

        .code-picture {
          width: 98px;
          height: 98px;
          flex-shrink: 0;

          img {
            width: 100%;
            height: 100%;
            object-fit: contain;
          }
        }

        .code-name {
          font-size: 18px;
          flex-shrink: 0;
          color: #333333;
          white-space: nowrap;
          overflow: hidden;
          width: calc(100% - 8px);
          margin-top: 12px;
          line-height: 1.2;
          text-align: center;
        }
      }

      .code-1 {
        margin-right: 12px;
      }

      .code-2 {
        margin-left: 12px;
      }
    }
  }

  .DRC-TCM-info {
    height: calc(100% - 166px - 14px);
    margin-top: 14px;
    padding: 0 30px;
    display: flex;
    flex-direction: column;
    box-sizing: border-box;

    .dept-room-number {
      height: 310px;
      width: calc(100% - 34px - 34px);
      border-radius: 12px;
      overflow: hidden;
      background: #8A6A36;
      font-weight: bold;
      font-size: 150px;
      color: #FFFFFF;
      display: flex;
      padding: 0 34px;
      margin-bottom: 38px;
      align-items: center;
      align-self: center;
      box-sizing: content-box;
      justify-content: center;

      &:before {
        content: '';
        height: 144px;
        width: 80px;
        @include background('design/left×2');
      }

      &:after {
        content: '';
        height: 144px;
        width: 80px;
        @include background('design/right×2');
      }

      .content {
        padding: 0 44px;
        box-sizing: content-box;
        font-size: 140px;
        width: calc(100% - 160px - 88px);

        :deep(.room-number) {
          font-size: 184px !important;
          padding-right: 20px !important;
        }

        :deep(.st-section) {
          margin-right: 60px;
        }
      }
    }

    .doctor-patient-info {
      margin-bottom: 30px;
      box-sizing: border-box;
      background: #E8E8E8;
      padding: 122px 110px;
      height: calc(100% - 310px - 38px - 30px);
      overflow: hidden;
      @include background('design/1-15×2');

      .doctor-info {
        width: 100%;
        height: calc(100% - 350px - 40px);
        margin-bottom: 40px;
        box-sizing: border-box;
        overflow: hidden;

        .doctor-brief {
          width: 100%;
          height: 300px;
          margin-bottom: 22px;
          box-sizing: border-box;
          overflow: hidden;
          display: flex;
          flex-direction: row;

          .left {
            display: flex;
            align-self: center;
            align-items: center;
            border-radius: 20px;
            overflow: hidden;
            width: 220px;
            border: none;
            height: 100%;
            margin-right: 40px;
            box-sizing: border-box;
            background: transparent;
            flex-shrink: 0;

            img {
              width: 100%;
            }
          }

          .right {
            width: calc(100% - 220px - 40px);

            .name {
              width: 100%;
              font-size: 104px;
              color: #8A6A36;
              line-height: 1.6;

              :deep(.st-inner) {
                margin: inherit;
              }
            }

            .tag-info {
              display: flex;
              align-items: center;
              align-self: center;
              align-content: center;

              .item {
                max-width: calc(100%);
                display: flex;
                align-self: center;
                align-items: center;
                background: #0098B8;
                padding: 8px 20px;
                border-radius: 8px;
                font-size: 50px;
                color: #FFFFFF;
                line-height: 1.4;
                box-sizing: border-box;
              }
            }
          }
        }

        .doctor-describe {
          height: calc(100% - 322px);
          width: 100%;
          font-size: 36px;
          color: #8A6A36;
          line-height: 1.6;
        }
      }

      .patient-info {
        height: 350px;
        display: flex;
        flex-direction: column;
        overflow: hidden;

        .patient-item {
          width: 100%;
          flex-grow: 1;
          border-radius: 4px;
          overflow: hidden;
          display: flex;
          align-items: center;
          height: 50%;
          align-self: center;
          background: #E8E8E8;

          .patient-sign {
            font-size: 60px;
            color: #FFFFFF;
            display: flex;
            height: 100%;
            padding: 0 36px;
            width: 240px;
            flex-shrink: 0;
            box-sizing: content-box;
            align-self: center;
            align-items: center;
            white-space: nowrap;
            overflow: hidden;
          }

          .patient-content {
            font-size: 70px;
            flex-shrink: 0;
            box-sizing: content-box;
            height: 100%;
            display: flex;
            align-self: center;
            align-items: center;
            width: calc(100% - 240px - 24px - 72px);
            white-space: nowrap;
            overflow: hidden;
            padding-left: 24px;
          }
        }

        .current-patient {
          margin-bottom: 20px;
          background: rgba(1, 118, 58, 0.2);

          .patient-sign {
            background: #01763A;
          }

          .patient-content {
            color: #01763A;
          }
        }

        .wait-patient {
          margin-top: 20px;
          background: rgba(138, 106, 54, 0.2);

          .patient-sign {
            background: #8A6A36;
          }

          .patient-content {
            color: #8A6A36;
          }
        }
      }
    }
  }
}
</style>
